
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;

import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;

import java.net.URL;


/**
 * @Auther: zhezhe
 * @Date: 2018/10/1617:22
 * @Description:
 */

public class HelloHDFS {
    public static void main(String[] args) {
        try {
            HelloHDFS.HDFS2();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void HDFS2() throws Exception{
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://master:9000");
        FileSystem fileSystem=FileSystem.get(conf);

        //在根目录下创建一个目录
        //需先判断目录存不存在，不存在再建，如果存在，则会覆盖原有目录
        boolean success = fileSystem.mkdirs(new Path("/meng"));
        System.out.println(success);
        //判断是否存在
        success = fileSystem.exists(new Path("/hello.txt"));
        System.out.println(success);
        //删除一个目录
        success=fileSystem.delete(new Path("/meng"),true);
        System.out.println(success);

        FSDataOutputStream out = fileSystem.create(new Path("/test.data"),true);


        FileInputStream fis = new FileInputStream("d:/test/core-site.xml");



        IOUtils.copyBytes(fis,out,4096,true);


        FSDataOutputStream out1 = fileSystem.create(new Path("/test.data"),true);
        FileInputStream in = new FileInputStream("d:/test/core-site.xml");
        byte[] buf = new byte[4096];
        int len = in.read(buf);
        while (len!=-1){
            out1.write(buf,0,len);
            len=in.read(buf);
        }
        in.close();
        out.close();


        FileStatus[] statuses = fileSystem.listStatus(new Path("/"));
        for (FileStatus status:statuses){
            System.out.println(status.getPath());
            System.out.println(status.getPermission());
            System.out.println(status.getReplication());
        }

    }

    public static void  HDFS() throws IOException {
        URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
        URL url = new URL("hdfs://192.168.220.20:9000/hello.txt");
        InputStream in = url.openStream();
        IOUtils.copyBytes(in,System.out,4096,true);
    }
//
//    public  static void FangWenWangZhi()  {
//        URL url = new URL("http://www.baidu.com");
//    }
}
